/* 省级联动 */
	var addIp = "http://1.14.68.137:8000";
	var province_id = $("#province_id");
	var city_id = $("#city_id");
	var county_id = $("#county_id");
	var jtid = $("#jtid")[0];
	var city_xz = $("#city_id>option").eq(0).clone(true);
	var county_xz = $("#county_id>option").eq(0).clone(true);
	// var addressIP = "";
	// var province_name = "";
	// var city_name = "";
	// var county_name = "";
	var province_opt = "";
	var city_opt = "";
	var county_opt = "";
	var city = "";
	var cityArr = "";
	var $race = "";
	var dataId = "";
	$.get(addIp+"/areas/", {}, function (x) {
		var data = x.data;
		for (var i = 0 ; i < data.length ; i++) {
			var $optCopy = $("#opt").clone(true);
			$optCopy.attr("id", "");
			$optCopy.attr("data-index", data[i].id);
			$optCopy.html(data[i].name);
			$("#province_id").append($optCopy[0]);
		};
		province_id.change(function () {
			province_opt = this.options[this.selectedIndex].getAttribute("data-index");
			// province_name = this.options[this.selectedIndex].value;
			county_id.html("");
			county_id.append(county_xz);
			addoption(province_opt, city_id, data, city_xz);
			cityArr = city
		});
		city_id.change(function () {
			city_opt = this.options[this.selectedIndex].getAttribute("data-index");
			// city_name = this.options[this.selectedIndex].value;
			addoption(city_opt, county_id, cityArr, county_xz);
			
		});
		county_id.change(function () {
			// county_name = this.options[this.selectedIndex].value;
			county_opt = this.options[this.selectedIndex].getAttribute("data-index");
		});
		function addoption(opt, twoNodeId, arrName, copyNode) {
			for (var i = 0 ; i < arrName.length ; i++) {
				if (opt == arrName[i].id) {
					city = arrName[i].children;
					twoNodeId.html("");
					twoNodeId.append(copyNode);
					for (var j = 0 ; j < city.length ; j++) {
						var $optCopy = $("#opt").clone(true);
						$optCopy.attr("id", "");
						$optCopy.attr("data-index", city[j].id);
						$optCopy.html(city[j].name);
						$(twoNodeId).append($optCopy);
					};
					break;
				};
			};
		};
	});
	var cookie = document.cookie
	/* 先截取到问号后面的所有字符串，不包括问号 */
	cookie = cookie.substring(cookie.indexOf("?") + 1);
	// 查看字符串，找找其中的规律，
	// 把字符串按&转换为数组
	var arr = cookie.split("; ");
	/* 定义一个对象 */
	var cookieObj = {};
	/* 使用for循环让转换为数组中的每个字符串转换为新创建对象的属性 */
	for (var i = 0 ; i < arr.length ; i++) {
		cookieObj[arr[i].split("=")[0]] = arr[i].split("=")[1];
	};
	$(".toaddress").click(function () {
		$(this).parent().parent().hide();
		$(".cont_add_ress").show();
		$(".edit").hide();
		$(".preser_").show();
		initialInput();
	});
	$(".return").click(function () {
		$(".raceadd").show();
		$(this).parent().parent().hide();
	});
	$("#whether").click(function () {
		$(this).attr("checked", !$(this).attr("checked"));
		$(this).attr("checked");
	})
/* 添加收货地址 */
	var addressObj = {};
	$(".preser_").click(function () {
		addressObj = {
			province_id: province_opt,
			city_id: city_opt,
			county_id: county_opt,
			detail: $("#jtid").val(),
			name: $("#ressee").val(),
			phone: $("#phone").val(),
			default: 1
		};
		if (!$("#whether").attr("checked")) {
			delete addressObj.default;
		}else {
			addressObj.default = 1;
		}
		$.ajax({
			url: addIp + "/address/",
			type: "POST",
			dataType: "json",
			data: addressObj,
			headers: {
				"token": cookieObj.token
			},
			success: function (x) {
				alert(x.msg);
			}
		})
		// $(".race").html("");
		// getAddress();
		initialInput();
	});
/* 获取收货地址 */
	getAddress();
	function getAddress() {
		$.ajax({
			url: addIp + "/address/",
			type: "GET",
			dataType: "json",
			headers: {
				"token": cookieObj.token
			},
			success: function (x) {
				var addData = x.data;
				var str = "";
				if (addData) {
					$.each(addData, function (i, e) {
						if (e.default == 1) {
							str = "是";
						}else {
							str = "否";
						}
						// $race = '<div class="race_infor cf" data-id="'+e.id+'"> \
						// 				<div class="name"><input type="text" /><span>'+e.name+'</span></div> \
						// 				<div class="phone"><input type="text" /><span>'+e.phone+'</span></div> \
						// 				<div class="address"><input type="text" /><span>'+e.province+' '+e.city+' '+e.county+' '+e.detail+'</span></div> \
						// 				<div class="default"><input type="text" /><span>'+str+'</span></div> \
						// 				<div class="operation"> \
						// 					<a class="update" href="javascript:;">编辑</a> \
						// 					<a class="delete" href="javascript:;">删除</a> \
						// 				</div> \
						// 			</div>';
						$race = '<div class="race_infor cf" data-id="'+e.id+'"> \
										<div class="name name_">'+e.name+'</div> \
										<div class="phone phone_">'+e.phone+'</div> \
										<div class="address">'+e.province+' '+e.city+' '+e.county+' '+e.detail+'</div> \
										<div class="default">'+str+'</div> \
										<div class="operation"> \
											<a class="update" href="javascript:;">编辑</a> \
											<a class="delete" href="javascript:;">删除</a> \
										</div> \
									</div>';
						$(".race").append($race);
					})
				}else {
					return
				}
			}
		})
	}
/* 删除收货地址 */
	$(".race").on("click", ".race_infor .delete", function () {
		var popup = confirm("您确定要删吗？");
		if (popup) {
			$(this).parent().parent().remove();
			dataId = $(this).parent().parent().attr("data-id");
			$.ajax({
				url: addIp + "/address/",
				type: "DELETE",
				dataType: "json",
				data: {id: dataId},
				headers: {
					"token": cookieObj.token
				},
				success: function (x) {
					console.log(x)
				}
			})
		}else {
			return
		}
	})
	
	
/* 正则验证 */
	var resseeReg = /^[A-z]{5,16}/;
	var phoneReg = /^1[3-9][0-9]{9}/;
/* 收件人验证 */
	$("#ressee").blur(function () {
		if ($(this).val()) {
			if ($(this).val().length > 6 && $(this).val().length < 2) {
				$(".regu_ress").html("收件人输入不规范，请重新输入！！！");
				$(".regu_ress").css({color: "#e05"});
				$(this).val("");
			}else {
				$(".regu_ress").html("通过");
				$(".regu_ress").css({color: "#00cc00"});
			}
		}
	})
/* 手机验证 */
	$("#phone").blur(function () {
		if ($(this).val()) {
			if (!phoneReg.test($(this).val())) {
				$(".regu_phone").html("手机号输入不规范，请重新输入！！！");
				$(".regu_phone").css({color: "#e05"});
				$(this).val("");
			}else {
				$(".regu_phone").html("通过");
				$(".regu_phone").css({color: "#00cc00"});
			}
		}
	})
/* 编辑 */
	var editId = "";
	$(".race").on("click", ".race_infor .update", function (){
		editId = $(this).parent().parent().attr("data-id");
		$(".toaddress").trigger("click");
		$("#ressee").val($(this).parent().parent().find(".name_").html())
		$("#phone").val($(this).parent().parent().find(".phone_").html())
		$(".preser_").hide();
		$(".edit").show();
	})
	$(".edit").click(function () {
		addressObj = {
			id: editId,
			province_id: province_opt,
			city_id: city_opt,
			county_id: county_opt,
			detail: $("#jtid").val(),
			name: $("#ressee").val(),
			phone: $("#phone").val(),
			default: 1
		};
		if (!$("#whether").attr("checked")) {
			delete addressObj.default;
		}else {
			addressObj.default = 1;
		}
		console.log(addressObj);
		$.ajax({
			url: addIp + "/address/",
			type: "PUT",
			dataType: "json",
			data: addressObj,
			headers: {
				"token": cookieObj.token
			},
			success: function (x) {
				console.log(x);
				alert(x.msg);
			}
		})
		initialInput();
	})
	
	
	$("#sgin").click(function () {
		location.href = "../sign_in.html";
	})
	
	function initialInput() {
		province_id.val(province_id.children().eq(0).val());
		city_id.val(city_id.children().eq(0).val());
		county_id.val(county_id.children().eq(0).val());
		$("#whether").attr("checked", false);
		$("#jtid").val("");
		$("#ressee").val("");
		$("#phone").val("");
		$(".regu_ress").html("");
		$(".regu_phone").html("");
	}
	
	